#pragma once

#include "../Utilities/utilities.h"
#include "HeightField.h"
#include "Entity.h"
//#include "Globals.h"

///All config for terrain please set into TerrainConfig.h
//Thanks

///This class is manage draw terrain
//	-Move
class Terrain : public Entity
{
private:
	char **m_aszFileTextureName;
	char *m_szFileHeightfeildName;
	unsigned int m_iTexureCount;
	HeightField *m_HeightData;

	float m_fDx;
	float m_fDz;
	float m_fWidth;
	float m_fHeight;

	unsigned short *indices;
public:
	///The height of map, it is a number of cell height in map, it is computed by MapRows = VertexRows - 1; 
	int MapRows;
	///The width of map, it is a number of cell width in map, it is computed by MapCols = VertexCols - 1;
	int MapColumns;
private:
	///Genarate map vertex positions
	//Input parameters:
	//	iVertexRows: Number of vertex count row
	//	iVertexCols: Number of vertex count column
	//	fDx: Distance between two vertex by x axis
	//	fDz: Distance between two vertex by z axis 
	//Output:
	//	Vertex data list
	///End
	Vertex* GenerateMapVertexPosition(int iVertexRows, int iVertexCols, int iCellRows, int iCellCols, float fDx, float fDz);

	///Generate map vertex indices
	unsigned short* GenerateMapVertexIndex(int iVertexRows, int iVertexCols, int iCellRows, int iCellCols, Vertex* aVertices);

	///Generate map vertex normals
	//Note: Not use outside GenerateMapVertexIndex
	//Input parameters:
	//	A,B,C: Three vertex of triangle
	void GenarateMapVertexNormal(Vertex &A, Vertex &B, Vertex &C);

	///Note: Can't use scale
	void Scale(GLfloat x, GLfloat y, GLfloat z){};
public:
	///Create input value
	//Input parameters:
	//	szFileHeightfeildName: File store heightfeild data
	//	aszFileTextureName: List name of texture will be drown in terrain
	//	textureCount: Number of file texture is added
	Terrain(char *szFileHeightfileName, char **aszFileTextureName, int textureCount);

	~Terrain();

	void Init();

	///Function check may be move into this cell
	//Input parameters:
	//	row, col: Cell position
	//Output:
	//	true: Can move
	//	false: Not move
	bool CanMove(int row, int col);

	///Function get height level of this cell
	//Input parameters:
	//	row, col: Cell position
	//Output:
	//	Not -1: Height level of this cell
	//	-1: Can get height level of this cell
	float GetHeight(int row, int col);

	virtual void draw();
	virtual void update(float deltatime);
	float Terrain::GetHeight(float x, float z);
};